package com.ygqh.baby.template;

import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: sunshuo
 * @date: 2018/12/20 16:10
 */
public abstract class AbstractSplitListTemplate<T> {

	private List<T> recordList;

	private int splitSize;

	public AbstractSplitListTemplate(List<T> recordList) {
		this(recordList, 30);
	}

	public AbstractSplitListTemplate(List<T> recordList, int splitSize) {
		this.recordList = recordList;
		this.splitSize = splitSize;
	}

	public int split() {
		int row = 0;
		if (CollectionUtils.isNotEmpty(recordList)) {
			List<T> list = new ArrayList<>(splitSize);
			for (int i = 0; i < recordList.size(); i++) {
				list.add(recordList.get(i));
				if (list.size() == splitSize || i == recordList.size() - 1) {
					row += execute(list);
					list.clear();
				}
			}
		}
		return row;
	}

	public abstract int execute(List<T> recordList);
}
